

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * 外网
 *关闭socket的流会导致socket关闭
 * Created by zhoub on 2019/12/9.
 */
public class VedioSocketServer {
    public static void main(String[] args) throws IOException {
        int port = 8081;

        ServerSocket serverSocket = new ServerSocket(port);
        while(true){
            final Socket socket = serverSocket.accept();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("has a client connect");
                    InputStream inputStream = null;
                    File file = new File("/data/app/cma/socket/test.mp4");
                    try {
                        OutputStream os = socket.getOutputStream();
//                        inputStream = socket.getInputStream();

                        Thread.sleep(1000*5);

                        //vedio flush
                        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                        byte[] bs = new byte[1024*1024*8]; //8MB
                        int len = 0;
                        while ((len = bis.read(bs)) != -1) {
                            System.out.println(len);
                            os.write(bs,0,len);
                        }
                        // close
                        bis.close();
                        os.flush();
                        os.close();

                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
